package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogTypeDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class BlogTypeDaoImpl implements IBlogTypeDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<BlogType> selectAll() {

        System.out.println("BlogTypeDaoImpl.selectAll");
        //写sql语句
        String sql = "SELECT * FROM blog_type";
        List<BlogType> list = template.query(sql, new BeanPropertyRowMapper<>(BlogType.class));
        return list;
    }

    @Override
    public List<BlogType> selectByPage(BlogTypeQuery blogTypeQuery) {
        List<Object> queryList = new ArrayList<>();

        String sql = "SELECT id,name,is_deleted,create_time,update_time FROM blog_type WHERE 1=1";
        /*String queryName = blogTypeQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }*/
        Integer queryTypeId = blogTypeQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        String beginCreateTime = blogTypeQuery.getBeginCreateTime();
        String endCreateTime = blogTypeQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null  && !"".equals(beginCreateTime) && !"".equals(endCreateTime)) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql+=" LIMIT ?,?";

        int offset = (blogTypeQuery.getPage()-1)*blogTypeQuery.getLimit();
        queryList.add(offset);
        queryList.add(blogTypeQuery.getLimit());

        return template.query(sql,new BeanPropertyRowMapper<>(BlogType.class),queryList.toArray());
    }

    @Override
    public int totalCount(BlogTypeQuery blogTypeQuery) {
        List<Object> queryList = new ArrayList<>();

        String sql = "SELECT count(*) FROM blog_type WHERE 1=1";
        /*String queryName = blogTypeQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }*/
        Integer queryTypeId = blogTypeQuery.getTypeId();
        if (queryTypeId != null && !"".equals(queryTypeId)) {
            sql += " AND type_id=?";
            queryList.add(queryTypeId);
        }
        String beginCreateTime = blogTypeQuery.getBeginCreateTime();
        String endCreateTime = blogTypeQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null ) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }


        return template.queryForObject(sql, Integer.class,queryList.toArray());
    }

    @Override
    public void add(BlogType blogType) {
        String sql = "INSERT INTO blog_type(name) VALUES (?)";
        template.update(sql,blogType.getName());

    }

    @Override
    public int deleteById(int id) {
        System.out.println("删除函数内传过来的id是："+id);

        int count = 0;

        String sql = "DELETE FROM blog_type WHERE id=?";
        count = template.update(sql,id);

        System.out.println("count:"+count);
        return count;
    }

    @Override
    public BlogType selectById(String id) {
        //1.写sql语句
        String sql = "SELECT * FROM blog_type WHERE id = ?";

        return template.queryForObject(sql,new BeanPropertyRowMapper<>(BlogType.class),id);
    }

    @Override
    public void update(BlogType blogType) {
        System.out.println("BlogTypeDaoImpl.update");
        //写sql语句
        String sql = "UPDATE blog_type SET name=? WHERE id=?";
        System.out.println("name:"+blogType.getName()+"id"+blogType.getId());
        template.update(sql,blogType.getName(),blogType.getId());
    }
}
